$(function() {
	//TODO 参数配置
	var orderList = {
		// post 发送数据
		data: null,
		// 参数设置
		config: {
			post: {
				page: 1,
				pagesize: 20,
				type: 'all' //pendingpay  alreadypay all disable 
			},
			page: {
				cpage: 1,
				totalpage: 1
			},
			query: {
				order_id: null,
				start_date: null,
				end_date: null
			}
		},
		// post 请求数据
		post: function(page) {
			//console.log(this.config.post);return;
			var _this = this;
			$.post('/worksReportOrder/ajaxOrderList.html', this.config.post, function(data) {
				data.data.list = resoLveCircle(data.data.list)
				var _data = data;
				_this.config.page.cpage = data.data.current_page;
				_this.config.page.totalpage = data.data.total_page;
				_this.data = {
					data: data.data.list
				}
				if(_this.config.page.totalpage != 1) {
					$('._page').pagination({
						pageCount: _this.config.page.totalpage,
						current: _this.config.page.cpage,
						coping: true,
						callback: function(data) {
							_this.config.post.page = data.getCurrent();
							_this.post();
						}
					});
				} else {
					$('._page').html('')
				}
				if(!_this.data.data[0]) {
					$('.order_list ._conter').html('暂无记录!');
					return false;
				}
				_this.setTab();
			})
			return this;
		},
		// 设置数据
		setTab: function(D) {
			var data = D || this.data;
			data.data[0].order_time;
			for(var i = 0; i < data.data.length; i++) {
				var time = new Date(data.data[i].order_time * 1000);
				var strTime = time.getFullYear() + '-' + setTime(time.getMonth() + 1) + '-' + setTime(time.getDate()) + ' ' + setTime(time.getHours()) + ':' + setTime(time.getMinutes()) + ':' + setTime(time.getSeconds());
				data.data[i].order_time = strTime;
			}

			function setTime(a) {
				if(a < 10) {
					return '0' + a
				} else {
					return a
				}
			}
			var html = template('test', data);
			$('.order_list ._conter').html(html);
		},
		// 添加事件
		setEvent: function() {
			//1、tab切换
			var _this = this;
			$('.certification_list a').click(function() {
				_this.config.post.page = 1;
				switch(this.id) {
					case 'one1':
						_this.config.post.type = 'all';
						break;
					case 'one2':
						_this.config.post.type = 'pendingpay';
						break;
					case 'one3':
						_this.config.post.type = 'alreadypay';
						break;
					case 'one4':
						_this.config.post.type = 'disable';
						break;
				}
				$('.certification_list a').removeClass('hr');
				$(this).addClass('hr');
				_this.post();
			});

			//取消订单
			$(window).on('click', '._example', function() {
					$("#LoginBox").fadeIn(300);
				})
				//关闭
			$(".close_btn").hover(function() {
				$(this).css({
					color: 'black'
				})
			}, function() {
				$(this).css({
					color: '#999'
				})
			}).on('click', function() {
				$("#LoginBox").fadeOut("fast");
			});
			//根据时间搜索
			$('._sou').click(function() {
				//查询
				//TODO 目前是查询全部订单
				$('.certification_list a.hr').removeClass('hr')
				$('.certification_list a:eq(0)').addClass('hr')
				_this.query();
			});
			$('._conter').on('click', '.payment a', function() {
				var order = $(this).parents('table').prev().find('span:eq(1) b').text();
				formSubmit({
					url: '/payreport/keeppay.html',
					data: {
						order_id: order
					},
					type: 'post'
				});
			})

		},
		init: function() { // 初始化数据
			this.setEvent();
			this.post();

			var i18n = { // 本地化
				previousMonth: '上个月',
				nextMonth: '下个月',
				months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
				weekdays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
				weekdaysShort: ['日', '一', '二', '三', '四', '五', '六']
			}

			var startTime = new Pikaday({
				field: $('._sTime')[0],
				i18n: i18n,
				onSelect: function() {
					$('._sTime').val(getDate(this._d, '-'));
				}
			});
			var endTime = new Pikaday({
				field: $('._eTime')[0],
				i18n: i18n,
				onSelect: function() {
					$('._eTime').val(getDate(this._d, '-'));
				}
			});
			// 解决从其他页面返回到本页面时 , 时间出现英文的问题
			var st = $('._sTime').val();
			var et = $('._eTime').val();
			st && $('._sTime').val(getDate(st, '-'));
			et && $('._eTime').val(getDate(et, '-'));
		},
		//TODO 查询
		query: function() {
			this.config.query.order_id = $('._number').val();			
			console.log(this.config)
			if(+new Date($('._eTime').val()) - +new Date($('._sTime').val()) < 0) {
				show_msg({
					msg: '开始时间不能大于结束时间',
					time: 2000,
					type: 'warning'
				})
				return false;
			}
			this.config.query.start_date = getDate($('._sTime').val(), '');
			this.config.query.end_date = getDate($('._eTime').val(), '');
			var _this = this;
			//TODO 发送的对象
			$.post('/worksReportOrder/ajaxOrderList.html', this.config.query, function(data) {
				//data = resoLveCircle(data)
				data.data.list = resoLveCircle(data.data.list)
				//console.log(data)
				var _data = data;
				_this.config.page.cpage = data.data.current_page;
				_this.config.page.totalpage = data.data.total_page;
				_this.data = {
						data: data.data.list
					}
					// cpage 当前页数
					// totalpage 总页数
				if(_this.config.page.totalpage != 1) {
					$('._page').pagination({
						pageCount: _this.config.page.totalpage,
						current: _this.config.page.cpage,
						coping: true,
						callback: function(data) {
							_this.config.post.page = data.getCurrent();
							_this.post();
						}
					});
				} else {
					$('._page').html('')
				}
				if(!_this.data.data[0]) {
					$('.order_list ._conter').attr({'style':'color:#000'})
					$('.order_list ._conter').html('暂无记录!');
					return false;
				}
				_this.setTab();
			})
		}
	}

	orderList.init();

});

function formSubmit(data) {
	var formInnHTML = ['<form style="display:none" action="' + (data.url || '#') + '" method="' + (data.type || 'post') + '">']
	for(var k in data.data) {
		formInnHTML.push('<input type="hidden" name="' + k + '" value="' + data.data[k] + '" />');
	}
	formInnHTML.push('<input type="submit" /></form>');
	var submitFrom = $(formInnHTML.join(''))[0];
	//  console.log(submitFrom);
	$('body').append(submitFrom);
	submitFrom.submit();
}

/**
 * 格式化时间
 * 如果没有分隔符 则返回 YYYYMMDD , 如果输入分隔符如'-'则返回YYYY-MM-DD
 * @param data 可以被new Date解析的时间数据
 * @param _ 分隔符
 * @returns {string}
 */
function getDate(data, _) {
	var time = new Date();
	switch(typeof data) {
		case 'number':
			time = new Date(+new Date() - data * 86400000);
			break;
		case 'string':
			time = new Date(data);
			break;
		case 'object':
			time = data;
			break;
	}

	var month = time.getMonth() + 1;
	var _data = time.getDate();
	if(month < 10) month = "0" + month;
	if(_data < 10) _data = "0" + _data;
	var year = [time.getFullYear(), month, _data];
	if(!(year[0] && year[1] && year[2])) {
		return "";
	}
	return year.join(_ || "");
} // 获得时间

//解析套餐周期
function resoLveCircle(dataArray) {
	//遍历数组，修改每一项的套餐周期
	for(var i = 0, len = dataArray.length; i < len; i++) {
		if(dataArray[i].condition_json)
			dataArray[i].cl = JSON.parse(dataArray[i].condition_json).package_cycle
	}
	return dataArray
}